package com.catbad.controller;

import com.catbad.annotations.Auth;
import com.catbad.domain.Film;
import com.catbad.services.FilmServices;
import com.catbad.vo.FilmPageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
public class AdminFilmController {
    @Autowired
    FilmServices filmServices;
    /**
     * 进入影片管理页面
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("adminfilm")
    public String adminCinema(String filmName,String languageCode,String typeCode,String countryCode, Integer pageNO, Integer rows, Model model){
        //得到页面相关数据
        FilmPageVO filmPageVO = filmServices.findPageForFilm(filmName,languageCode,typeCode,countryCode,pageNO,rows);
        model.addAttribute("filmPageVO",filmPageVO);
        return"admin/film";
    }

    /**
     * 影片添加
     * @param file
     * @param film
     * @param request
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("addFilm")
    @ResponseBody
    public Map<String, Object> saveFilm(MultipartFile file, Film film, HttpServletRequest request){
        Map<String,Object> ret = new HashMap<>();
        try {
            if(!file.isEmpty()){
                String path = request.getSession().getServletContext().getRealPath("/");
                String imgName = UUID.randomUUID()+file.getOriginalFilename();
                String imgPath = path+"img\\"+imgName;
                File fi = new File(imgPath);
                file.transferTo(fi);
                film.setFilmImg(imgName);
            }
            if(     film.getFilmName()!=null
                    && film.getFilmImg()!=null
                    && film.getCountry()!=null
                    && film.getTypes()!=null
                    && film.getLanguage()!=null
                    && film.getLength()!=null
                    && film.getSynopsis()!=null
                    && film.getStars()!=null
                    && film.getDirector()!=null
                    && film.getPlayTime()!=null
            ) {
                film.setFilmDeleteFlag("1");
                Integer flag = filmServices.saveFilm(film);
                if(flag!=null && flag!=0){
                    ret.put("code",1);
                    ret.put("msg","成功");
                }else{
                    ret.put("code",0);
                    ret.put("msg","添加失败");
                }
            }else{
                ret.put("code",2);
                ret.put("msg","有为空项,请重新选择图片");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 处理layui图片组件单独请求的问题
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("img")
    @ResponseBody
    public Map<String, Object> img(){
        Map<String,Object> ret = new HashMap<>();
        ret.put("code",1);
        ret.put("msg","添加成功");
        return ret;
    }

    /**
     * 删除影片
     * @param fid
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("filmDelete")
    @ResponseBody
    public Map<String, Object> filmDelete(Long fid){
         Map<String,Object> ret = new HashMap<>();
         int code = filmServices.deleteFilm(fid);
         ret.put("code",code);
         return ret;
    }
    /**
     * 获得编辑时的页面信息
     * @param fid
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("getFilm")
    @ResponseBody
    public Film fetFilm(Long fid){
        return filmServices.getFilmById(fid);
    }

    /**
     * 修改影片信息
     * @param film
     * @return
     */
    @Auth("com.catbad.admin.film")
    @RequestMapping("setFilm")
    @ResponseBody
    public Map<String, Object> setFilm(MultipartFile file, Film film, HttpServletRequest request){
        //修改信息，1.判断图片是否更改，是就将存储图片，删除原来的图片，将修改的路径存入film，否就为null，就不需要修改
        Map<String,Object> ret = new HashMap<>();
        String path = null;
        try {
            if(!file.isEmpty()){
                path = request.getSession().getServletContext().getRealPath("/");
                String imgName = UUID.randomUUID()+file.getOriginalFilename();
                String imgPath = path+"img\\"+imgName;
                File fi = new File(imgPath);
                file.transferTo(fi);
                film.setFilmImg(imgName);
            }
            if(     film.getFilmName()!=null
                    && film.getCountry()!=null
                    && film.getFilmId()!=null
                    && film.getTypes()!=null
                    && film.getLanguage()!=null
                    && film.getLength()!=null
                    && film.getSynopsis()!=null
                    && film.getStars()!=null
                    && film.getDirector()!=null
                    && film.getPlayTime()!=null
            ) {
                Integer flag = filmServices.setFilm(film,path);
                if(flag!=null && flag!=0){
                    ret.put("code",1);
                    ret.put("msg","修改成功");
                }else{
                    ret.put("code",0);
                    ret.put("msg","修改失败");
                }
            }else{
                ret.put("code",2);
                ret.put("msg","有为空项,请重新选择图片");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

}
